<template>
  <el-row :gutter="20" justify="center" align="middle" class="login-row">
    <el-col :span="8" :offset="0">
      <el-card shadow="always" :body-style="{ padding: '20px' }">
        <template #header>
          <div>
            <h3>讯美家具管理系统</h3>
          </div>
        </template>
        <!-- card body -->
        <el-form
          ref="ruleFormRef"
          style="max-width: 600px"
          :model="ruleForm"
          status-icon
          :rules="rules"
          label-width="auto"
          class="demo-ruleForm"
        >
          <el-form-item label="用户账号" prop="username">
            <el-input v-model="ruleForm.username" />
          </el-form-item>
          <el-form-item label="用户密码" prop="password">
            <el-input
              v-model="ruleForm.password"
              type="password"
              autocomplete="off"
            />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm(ruleFormRef)">
              登录
            </el-button>
            <el-button @click="resetForm(ruleFormRef)">Reset</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </el-col>
  </el-row>
</template>

<script setup>
import { reactive, ref } from "vue";
import { userLogin, userReg } from "../../api/user";
import { ElMessage } from "element-plus";
import { useAccount } from "../../store/user";
import { useRouter } from "vue-router";

const ruleFormRef = ref();

const checkAge = (rule, value, callback) => {
  if (!value) {
    return callback(new Error("请输入账号")); //表单验证失败
  }
  callback(); //通过验证
};

const validatePass = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("Please input the password"));
  } else {
    if (ruleForm.checkPass !== "") {
      if (!ruleFormRef.value) return;
      ruleFormRef.value.validateField("checkPass");
    }
    callback();
  }
};
const validatePass2 = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("Please input the password again"));
  } else if (value !== ruleForm.password) {
    callback(new Error("Two inputs don't match!"));
  } else {
    callback();
  }
};
// 表单数据
const ruleForm = reactive({
  password: "",
  checkPass: "",
  username: "",
});
//表单验证逻辑
const rules = reactive({
  password: [{ validator: validatePass, trigger: "blur" }],
  checkPass: [{ validator: validatePass2, trigger: "blur" }],
  username: [{ validator: checkAge, trigger: "blur" }],
});

// 表单提交事件
const account = useAccount(); //获取用户信息状态机模块
const router = useRouter(); //获取触发路由跳转的方法
const submitForm = (formEl) => {
  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (valid) {
      //验证通过
      console.log("submit!", ruleForm);
      // 方法1：
      // let msg = await userLogin(ruleForm); //组件中触发登录请求（在卧室中点餐）
      // ElMessage(msg); //提示注册结果
      // 方法2：
      let msg = await account.loginAsync(ruleForm); // 在组件中触发状态机，由状态机触发登录请求
      ElMessage(msg); //提示注册结果
      if (msg.type == "success") {
        router.push("/"); //路由跳转主面板
      }
    } else {
      console.log("error submit!");
    }
  });
};

const resetForm = (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>

<style>
.login-row {
  height: 100vh;
}
.login-row h3 {
  text-align: center;
}
</style>
